package com.srwl.mytx.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.CustomViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.ViewTarget;
import com.bumptech.glide.request.transition.Transition;
import com.srwl.mytx.R;
import com.srwl.mytx.domain.AliOssService;
import com.srwl.mytx.utils.GlideLoadUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Adapter
 * Created by Yancy on 2015/12/4.
 */
public class PictureAdapter extends RecyclerView.Adapter<PictureAdapter.ViewHolder> {
    private static final String TAG = "PictureAdapter";
    private Context context;
    private LayoutInflater mLayoutInflater;
    private List<String> picturesUrl;
    //屏幕宽度
    private int screenWidth;

    public PictureAdapter(Context context, List<String> picturesUrl) {
        mLayoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.picturesUrl = picturesUrl;
        screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        Log.d("PictureAdapter", "屏幕宽度：" + screenWidth);
    }

    public interface OnItemClickListener {
        void onDeleteClick(View view, int position);

        void onImageClick(RecyclerView.ViewHolder viewHolder, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mLayoutInflater.inflate(R.layout.tx_recycle_item_goods_picture, null));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL);
        // Glide.with(context).load(picturesUrl.get(position)).apply(options).into(holder.image);
        String url = picturesUrl.get(position);
        String remoteUrl;
        if (url != null && url.startsWith("http")) {
            remoteUrl = url;
        } else {
            remoteUrl = AliOssService.TOPIC_IMAGE_PRE_URL + url;
        }
        // GlideApp.with(context).load(remoteUrl).diskCacheStrategy(DiskCacheStrategy.ALL).into(holder.image);
        if (GlideLoadUtils.isActivityAlive((Activity) context)) {

            Glide.with(context).asBitmap().load(remoteUrl).apply(options).into(new CustomTarget<Bitmap>() {//thumbnail(0.5f).

                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                    final float imgWidth = resource.getWidth();
                    final float imgHeight = resource.getHeight();

                    holder.image.setLayoutParams(fitImage(screenWidth, holder.image, imgWidth, imgHeight));
                    holder.image.setImageBitmap(resource);

                    Log.d(TAG, "onResourceReady: 图片加载完成position:" + position + "imgWidth:" + imgWidth + " imgHeight   " + imgHeight);
                }

                @Override
                public void onLoadCleared(@Nullable Drawable placeholder) {

                }


//
//            @Override
//            public void onResourceReady(@NonNull final Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
//                final float imgWidth = resource.getWidth();
//                final float imgHeight = resource.getHeight();
//
//                holder.image.setLayoutParams(fitImage(screenWidth, holder.image, imgWidth, imgHeight));
//                holder.image.setImageBitmap(resource);
//
//                Log.d(TAG, "onResourceReady: 图片加载完成position:" + position + "imgWidth:" + imgWidth + " imgHeight   " + imgHeight);
//
//            }
            });
        }


    }

    @Override
    public int getItemCount() {
        return picturesUrl.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.iv_goods_picture)
        ImageView image;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }

    public static RelativeLayout.LayoutParams fitImage(int width, ImageView imageView, float picWidth, float picHeight) {

        int height = (int) (width / picWidth * picHeight);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
//        layoutParams.width = width;
//        layoutParams.height = (int) height;

        return layoutParams;
    }

}
